package com.zh.proxymode.services.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//代理类，代理任何对象（使用反射）来实现Sell的功能
public class DynamicIntermediary implements InvocationHandler {

    private Object target;

    public Object getInstance(Object target){
        this.target = target;

        Class<?> cla = target.getClass();
        return Proxy.newProxyInstance(cla.getClassLoader(), cla.getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object obj = method.invoke(this.target,args);
        ShowHouse();
        SellHouse();
        return obj;
    }

    private void ShowHouse(){
        System.out.println("带客户看房子");
    }

    private void SellHouse(){
        System.out.println("办过户手续，抽取佣金");
    }
}
